package com.copm.ifm.servers.sys.service.impl.process;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.copm.ifm.base.basic.constant.DbConstants;
import com.copm.ifm.base.basic.util.AuthUtils;
import com.copm.ifm.servers.sys.entity.SysSetting;
import com.copm.ifm.servers.sys.mapper.process.SysSettingMapper;
import com.copm.ifm.servers.sys.service.msg.SysSettingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 消息发送接口 服务实现类
 *
 * @author tp
 * @since 2021-01-04
 */
@Service
public class SysSettingServiceImpl extends ServiceImpl<SysSettingMapper, SysSetting> implements SysSettingService {

    @Resource
    SysSettingMapper sysSettingMapper;

    @Override
    public List<SysSetting> selectByType(String type) {
        return sysSettingMapper.selectByType(type);
    }

    @Override
    public void saveListStr(String entityListStr) {
        Long tenantId = AuthUtils.getCurrentUserTenant();
        Long userId = AuthUtils.getCurrentUserId();
        JSONArray jsonArray = JSONArray.parseArray(entityListStr);
        List<SysSetting> list = jsonArray.toJavaList(SysSetting.class);
        if (list.size() > 0) {
            list.forEach(s -> {
                s.setEnabled(DbConstants.DEFAULT_YES);
                s.setCreateBy(userId);
                s.setUpdateBy(userId);
                s.setTenantId(tenantId);
            });
        }
        saveBatch(list);
    }

    @Override
    public SysSetting selectById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public int updateDeleted(Long id) {
        SysSetting sysSetting = new SysSetting();
        sysSetting.setId(id);
        sysSetting.setDeleted(DbConstants.DEFAULT_YES);
        return sysSettingMapper.updateById(sysSetting);
    }

    @Override
    public Map<String, SysSetting> selectNavAndCSSHead() {
        List<SysSetting> sysSettingList = sysSettingMapper.selectNavAndCSShead();
        return sysSettingList.stream().collect(Collectors.toMap(SysSetting::getCode, Function.identity()));
    }


}
